﻿using Caltech.Models;
using CaltechSoft.Service.ReportService.WorkOrder;
using CaltechSoft.Service.CommandService.WorkOrder;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CaltechSoft.Service.SearchCriteria;
using AutoMapper;
using CaltechSoft.Service.EntityFW.Entity;
using Microsoft.AspNet.Identity;
using Caltech.Results;
using Caltech.Extensions;
using Caltech.Service.Common;

namespace Caltech.Controllers
{
    [HasAccess(Areas.WOView)]
    public class WorkOrderController : ApiController
    {
        private readonly IWorkOderCommandService _workOrderCommandService;
        private readonly IWorkOrderReportService _workOrderReportService;

        public WorkOrderController(IWorkOderCommandService workOrderCommandService, IWorkOrderReportService workOrderReportService)
        {
            _workOrderCommandService = workOrderCommandService;
            _workOrderReportService = workOrderReportService;
        }

        // GET api/<controller>
        [HasAccess(Areas.WOView)]
        public IHttpActionResult Get(string customerName = null, int? customerId = null, string salesman = null)
        {
            var pageSize = Convert.ToInt32(Request.GetQueryNameValuePairs().FirstOrDefault(n => n.Key == "count").Value);
            var pageIndex = Convert.ToInt32(Request.GetQueryNameValuePairs().FirstOrDefault(n => n.Key == "page").Value) - 1;
            var saleman = Request.GetQueryNameValuePairs().FirstOrDefault(n => n.Key == "salesman").Value;

            var searchCriteria = new WorkOrderSearchCriteria();
            searchCriteria.CustomerId = customerId;
            searchCriteria.CustomerName = customerName;
            searchCriteria.Saleman = saleman;

            var result = _workOrderReportService.Search(searchCriteria, pageSize, pageIndex);

            return Request.CreateSearchResult<WorkOrderSearchModel>(result.Items.Select(x => new WorkOrderSearchModel()
                {
                    WorkOrderId = x.WorkOrderId,
                    WorkOrderNo = x.WorkOrderNo,
                    PONumber = x.PONumber,
                    CompanyName = x.CustomerName,
                    SalesmanName = x.SalesmanName,
                    CalibrationDate = x.CalibrationDate,
                    NextDueDate = x.NextDueDate,
                    Status = x.Status,
                    TotalUnit = GetTotalUnit(x.WorkOrderId),
                    Note = x.Note
                }), result.Count);
        }

        private int GetTotalUnit(int woId)
        {
            var wo = _workOrderReportService.SearchById(woId);
            var totalUnit = wo.WorkOrderDetails.Sum(n => n.Quantity);
            return totalUnit;
        }

        // GET api/<controller>/5
        [HasAccess(Areas.WOView)]
        public IHttpActionResult Get(int id)
        {
            var result = _workOrderReportService.SearchById(id);
            var mainResult = Mapper.Map<WorkOrderModel>(result);
            return Ok(mainResult);
        }

        [HasAccess(Areas.WOCreate)]
        public int Post(WorkOrderModel model)
        {
            var entity = Mapper.Map<WorkOrder>(model);

            entity.CreatedDate = DateTime.Now;

            return _workOrderCommandService.CreateWorkOrder(entity);

        }

        // PUT api/<controller>/5
        [HasAccess(Areas.WOEdit)]
        public void Put(int id, WorkOrderModel model)
        {
            model.User = null;

            var entity = Mapper.Map<WorkOrder>(model);

            entity.UpdatedDate = DateTime.Now;

            _workOrderCommandService.UpdateWorkOrder(entity);
        }

        // DELETE api/<controller>/5
        [HasAccess(Areas.WODelete)]
        public void Delete(int id)
        {
            _workOrderCommandService.Delete(id);
        }
    }
}